const TIME_OUT = 20000;

export default function handleRequest(options) {
  return new Promise((resolve, reject) => {
    uni.request({
      method: options.method,
      timeout: options.timeout || TIME_OUT,
      url: options.baseUrl + options.url,
      data: options.data,
      header: options.headers,
      dataType: "json",
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res);
        } else {
          const { data: error } = res;
          error.message = error.error;
          Reflect.deleteProperty(error, "error");
          reject(error);
        }
      },
      fail: (err) => reject(err),
    });
  });
}
